#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "MyGlWindow.h"
#include "LampDlg.h"
#include "LampProg.h"
#include "MaterialDlg.h"
#include "MaterialProg.h"
#include <QMessageBox>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    glWnd(new MyGlWindow)
{
    ui->setupUi(this);

    format.setMajorVersion(3);
    format.setMinorVersion(3);
    format.setProfile(QSurfaceFormat::CoreProfile);

    glWnd->setFormat(format);
    delete ui->centralWidget;

    ui->centralWidget = QWidget::createWindowContainer(glWnd, this);
    setCentralWidget(ui->centralWidget);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionLamp_triggered()
{
    LampProg* prog = glWnd->lamp();

    if (! prog)
        return;

    LampDlg* dlg = new LampDlg;
    dlg->setLampColor(prog->lampColor());

    connect(dlg,
            SIGNAL(lampColorChanged(const QVector3D&)),
            prog,
            SLOT(setLampColor(const QVector3D&)));

    dlg->show();
}

void MainWindow::on_actionCube_triggered()
{
    MaterialProg* prog = glWnd->material();

    if (! prog)
        return;

    MaterialDlg* dlg = new MaterialDlg;
    dlg->initMaterial(prog->material());

    connect(dlg,
            SIGNAL(materialChanged(const Material&)),
            prog,
            SLOT(setMaterial(const Material&)));

    dlg->show();
}

void MainWindow::on_actionLight_triggered()
{

}

void MainWindow::on_actionAboutOpenGL_triggered()
{
    QMessageBox::information(this, tr("OpenGL context"), glWnd->contextInformation());
}
